﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Managers.*;
    import flash.display.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.sim.*;
    import __AS3__.vec.*;
    import Classes.effects.*;
    import Display.*;
    import Classes.bonus.*;
    import Classes.actions.*;
    import Modules.stats.types.*;
    import com.zynga.skelly.util.*;
    import Modules.quest.Helpers.*;

    public class Mall extends MechanicMapResource implements IMerchant {

        protected static const PEEP_STATE_SPAWN:String = "PeepStateSpawning";
        protected static const PEEP_STATE_BLOCK:String = "PeepStateBlock";

        private static var intTourBusNPCCount:int = 0;

        protected var m_crowdManager:MerchantCrowdManager = null;
        protected var m_peepState:String = "PeepStateBlock";
        protected var m_coinPickEffect:CoinPickEffect = null;
        protected var m_tourBus:TourBus;
        protected var m_floorToMysteryBusinessMap:Dictionary;

        public function Mall(_arg1:String){
            super(_arg1);
            this.m_crowdManager = new MerchantCrowdManager(this, _arg1);
            m_objectType = WorldObjectTypes.MALL;
            m_isRoadVerifiable = true;
            this.m_peepState = PEEP_STATE_BLOCK;
        }
        override public function loadObject(_arg1:Object):void{
            var _local2:int;
            var _local3:Object;
            var _local4:ItemInstance;
            super.loadObject(_arg1);
            m_npcPopularity = 0;
            mechanicData["slots"] = new Array();
            if (((_arg1.mechanicData.slots) && (!((_arg1.mechanicData.slots == NOT_INITIALIZED_MECHANIC))))){
                while (_local2 < _arg1.mechanicData.slots.length) {
                    _local3 = (_arg1.mechanicData.slots[_local2]) ? _arg1.mechanicData.slots[_local2] : null;
                    if (_local3){
                        _local4 = (Global.world.loadObjectInstance(_local3) as ItemInstance);
                        _local4.loadObject(_local3);
                        (mechanicData["slots"] as Array).push(_local4);
                        if ((_local4 is MapResource)){
                            m_npcPopularity = (m_npcPopularity + (_local4 as MapResource).npcPopularity);
                        };
                    };
                    _local2++;
                };
            };
            if (_arg1.mechanicData.harvestState){
                mechanicData["harvestState"] = _arg1.mechanicData.harvestState;
            };
            if (this.isOpen()){
                this.updatePeepSpawning();
            };
        }
        override public function sell():void{
            if (this.canFinishSell()){
                super.sell();
            } else {
                UI.displayMessage(ZLoc.t("Main", "CannotSellStorageItem", {item:getItemFriendlyName()}));
            };
        }
        private function canFinishSell():Boolean{
            var _local1:Array = (mechanicData["slots"] as Array);
            return (((!(_local1)) || ((_local1.length == 0))));
        }
        public function updatePeepSpawning():void{
            var _local1 = (this.m_peepState == PEEP_STATE_SPAWN);
            var _local2 = (this.m_peepState == PEEP_STATE_BLOCK);
            var _local3:Boolean = this.isOpen();
            var _local4 = !(_local3);
            if (((_local3) && (!(isNeedingRoad)))){
                Global.world.citySim.npcManager.startSpawningBusinessPeeps();
            };
            if (((_local4) || (isNeedingRoad))){
            };
            if (((((_local4) && (_local1))) || (isNeedingRoad))){
                Global.world.citySim.npcManager.onBusinessClosed();
            };
            this.m_peepState = PEEP_STATE_BLOCK;
            if (_local3){
                this.m_peepState = PEEP_STATE_SPAWN;
            };
        }
        public function isOpen():Boolean{
            var _local1:Boolean;
            if (((mechanicData["harvestState"]) && (mechanicData["harvestState"]["customersReq"]))){
                _local1 = (mechanicData["harvestState"]["customers"] < mechanicData["harvestState"]["customersReq"]);
            };
            return (_local1);
        }
        public function isRouteable():Boolean{
            var _local1:Boolean;
            if (((((this.isOpen()) && (!(isNeedingRoad)))) && ((this.getPopularity() > 0)))){
                _local1 = true;
            };
            return (_local1);
        }
        public function hasHotspot():Boolean{
            var _local1:Boolean;
            if (null != getHotspot()){
                _local1 = true;
            };
            return (_local1);
        }
        public function getMapResource():MapResource{
            return (this);
        }
        public function get crowdManager():MerchantCrowdManager{
            return (this.m_crowdManager);
        }
        public function getPopularity():int{
            return (this.npcPopularity);
        }
        public function setPopularity(_arg1:int):void{
            this.npcPopularity = _arg1;
        }
        override public function isVisitorInteractable():Boolean{
            var _local1:Dictionary = getMechanicData();
            if (((((((isNeedingRoad) || (isPendingOrder))) || ((_local1 == null)))) || ((_local1["harvestState"] == null)))){
                return (false);
            };
            return (true);
        }
        public function get slots():Array{
            var _local1:Dictionary = getMechanicData();
            if ((((_local1 == null)) || ((_local1["slots"] == null)))){
                _local1["slots"] = new Array();
            };
            return (_local1["slots"]);
        }
        public function set slots(_arg1:Array):void{
            var _local2:Dictionary = getMechanicData();
            _local2["slots"] = _arg1;
        }
        public function get visitors():Dictionary{
            var _local4:String;
            var _local5:String;
            var _local1:Dictionary = new Dictionary();
            var _local2:Dictionary = getMechanicData();
            if ((((_local2 == null)) || ((_local2["harvestState"] == null)))){
                return (null);
            };
            var _local3:Object = _local2["harvestState"]["visitors"];
            for (_local4 in _local3) {
                _local5 = _local4;
                if (_local5.indexOf("i") >= 0){
                    _local5 = _local5.substr(1);
                };
                _local1[_local5] = _local3[_local4];
            };
            return (_local1);
        }
        public function set visitors(_arg1:Dictionary):void{
            var _local3:String;
            var _local4:String;
            if ((((mechanicData == null)) || ((mechanicData["harvestState"] == null)))){
                return;
            };
            var _local2:Object = new Object();
            for (_local3 in _arg1) {
                _local4 = _arg1[_local3];
                _local2[("i" + _local3)] = _local4;
            };
            mechanicData["harvestState"]["visitors"] = _local2;
        }
        override public function getToolTipStatus():String{
            var _local2:Object;
            var _local3:int;
            var _local4:int;
            var _local1:String;
            if (isNeedingRoad){
                _local1 = ZLoc.t("Dialogs", "NotConnectedToRoad");
                return (_local1);
            };
            if (mechanicData["harvestState"]){
                _local2 = mechanicData["harvestState"];
                if (((((!((_local2["customers"] == null))) && (!((_local2["customersReq"] == null))))) && (_local2["resources"]))){
                    _local3 = _local2["customers"];
                    _local4 = _local2["customersReq"];
                    _local1 = ZLoc.t("Main", "BusinessCustomers", {
                        served:_local3,
                        max:_local4
                    });
                    return (_local1);
                };
            };
            return (super.getToolTipStatus());
        }
        override public function onObjectDrop(_arg1:Vector3):void{
            super.reloadImage();
            super.onObjectDrop(_arg1);
            if (!isNeedingRoad){
                this.updatePeepSpawning();
                this.displayStatus(ZLoc.t("Main", "ConnectedToRoad"), "", 0xAA00);
            };
        }
        override public function onUpdate(_arg1:Number):void{
            super.onUpdate(_arg1);
            this.m_crowdManager.onUpdate(_arg1);
        }
        override public function updateRoadState():void{
            super.updateRoadState();
            this.updatePeepSpawning();
            this.updateEffects();
        }
        public function getMysteryBusinessItemName(_arg1:int):String{
            return (this.floorToMysteryBusinessMap[_arg1]);
        }
        public function get floorToMysteryBusinessMap():Dictionary{
            var _local1:Item;
            var _local2:String;
            var _local3:Array;
            var _local4:int;
            var _local5:Item;
            var _local6:String;
            if (this.m_floorToMysteryBusinessMap == null){
                this.m_floorToMysteryBusinessMap = new Dictionary();
                _local1 = getItem();
                _local2 = Item.findUpgradeRoot(_local1);
                _local3 = UpgradeDefinition.getFullUpgradeChain(_local2);
                _local4 = 0;
                while (_local4 < _local3.length) {
                    _local5 = _local3[_local4];
                    _local6 = _local5.floorMysteryBusiness;
                    this.m_floorToMysteryBusinessMap[_local4] = _local6;
                    _local4++;
                };
            };
            return (this.m_floorToMysteryBusinessMap);
        }
        public function hasMysteryBusinessInInventory(_arg1:int):Boolean{
            var _local2:String = this.getMysteryBusinessItemName(_arg1);
            return ((Global.player.inventory.getItemCountByName(_local2) > 0));
        }
        override public function onUpgrade(_arg1:Item, _arg2:Item, _arg3:Boolean=true):void{
            super.onUpgrade(_arg1, _arg2);
            this.updatePeepSpawning();
            StatsManager.sample(100, StatsCounterType.GAME_ACTIONS, "mall", "upgrade_from", _arg1.name);
            StatsManager.sample(100, StatsCounterType.GAME_ACTIONS, "mall", "upgrade_to", _arg2.name);
        }
        public function onWaveFinished():void{
        }
        override public function onVisitPlayAction():void{
            if (((isNeedingRoad) || (isPendingOrder))){
                return;
            };
            m_actionMode = VISIT_PLAY_ACTION;
            m_numVisitorInteractions++;
            this.doTourBus();
            trackVisitAction(TrackedActionType.SENT_BUS);
            NeighborVisitManager.setVisitFlag(NeighborVisitManager.VISIT_TOURBUS);
        }
        override public function onVisitReplayAction(_arg1:TRedeemVisitorHelpAction):Number{
            var _local2:Number = super.onVisitReplayAction(null);
            GameTransactionManager.addTransaction(_arg1);
            this.doTourBus();
            return (_local2);
        }
        protected function doTourBus():void{
            var loopsound:* = null;
            var rep:* = 0;
            var coins:* = 0;
            lockForReplay();
            Sounds.play("tourbus_start");
            loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
            intTourBusNPCCount++;
            removeStagePickEffect();
            var nearRoad:* = Global.world.citySim.roadManager.findClosestRoad(getPosition());
            if (!nearRoad){
                if (Global.isVisiting()){
                    rep = Global.gameSettings.getInt("friendVisitBusinessRepGain", 1);
                    coins = Global.gameSettings.getNumber("friendHelpDefaultCoinReward", 10);
                    finalizeAndAwardVisitorHelp(VisitorHelpType.BUSINESS_SEND_TOUR, rep, coins);
                };
                return;
            };
            var startRoad:* = Global.world.citySim.roadManager.findRandomRoadOnScreen(nearRoad);
            this.m_tourBus = new TourBus(Business.TOUR_BUS, false);
            this.m_tourBus.setPosition(startRoad.getHotspot().x, startRoad.getHotspot().y);
            Global.world.citySim.npcManager.addVehicle(this.m_tourBus);
            var actions:* = this.m_tourBus.getStateMachine();
            actions.removeAllStates();
            actions.addActions(new ActionAnimationEffect(this.m_tourBus, EffectType.VEHICLE_POOF), new ActionNavigate(this.m_tourBus, this, startRoad).setPathType(RoadManager.PATH_ROAD_ONLY), new ActionFn(this.m_tourBus, Curry.curry(function (_arg1:Mall):void{
                _arg1.m_tourBus.full = false;
                _arg1.unloadTourists();
            }, this)), new ActionFn(this.m_tourBus, function ():void{
                Sounds.stop(loopsound);
                Sounds.play("tourbus_honk");
                Sounds.play("peopleMoveIn");
            }), new ActionProgressBar(this.m_tourBus, this, ZLoc.t("Main", "UnloadingTourists")), new ActionFn(this.m_tourBus, function ():void{
                var _local1:*;
                var _local2:*;
                if (Global.isVisiting()){
                    _local1 = Global.gameSettings.getInt("friendVisitBusinessRepGain", 1);
                    _local2 = Global.gameSettings.getNumber("friendHelpDefaultCoinReward", 10);
                    finalizeAndAwardVisitorHelp(VisitorHelpType.BUSINESS_SEND_TOUR, _local1, _local2);
                };
                Sounds.play("tourbus_start");
                loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
                unlockForReplay();
            }), new ActionNavigate(this.m_tourBus, startRoad, this), new ActionFn(this.m_tourBus, function ():void{
                Sounds.stop(loopsound);
                Sounds.play("tourbus_stop");
            }), new ActionDie(this.m_tourBus));
            displayStatus(ZLoc.t("Main", "SendingTourists"));
        }
        public function getInitialHarvestBonusForFloor(_arg1:int):int{
            var _local7:HarvestBonus;
            var _local2:String = Item.findUpgradeRoot(getItem());
            var _local3:Array = UpgradeDefinition.getFullUpgradeChain(_local2);
            if ((((_local3 == null)) || ((_arg1 >= _local3.length)))){
                return (0);
            };
            var _local4:Item = _local3[_arg1];
            var _local5:Vector.<HarvestBonus> = _local4.harvestBonuses;
            var _local6:int;
            for each (_local7 in _local5) {
                _local6 = (_local6 + _local7.initialPercentModifier);
            };
            return (_local6);
        }
        protected function createCoinPickEffect():void{
            if (!this.m_coinPickEffect){
                this.m_coinPickEffect = (MapResourceEffectFactory.createEffect(this, EffectType.COIN_PICK) as CoinPickEffect);
                this.m_coinPickEffect.setPickType(CoinPickEffect.PICK_1);
            } else {
                this.m_coinPickEffect.setPickType(CoinPickEffect.PICK_1);
                this.m_coinPickEffect.reattach();
            };
        }
        override public function cleanUp():void{
            if (this.m_coinPickEffect){
                this.m_coinPickEffect.cleanUp();
            };
            super.cleanUp();
        }
        override protected function updateArrow():void{
            super.updateArrow();
            if (((isNeedingRoad) || (isUxLockedByQuests))){
                if (this.m_coinPickEffect){
                    this.m_coinPickEffect.cleanUp();
                    this.m_coinPickEffect = null;
                };
                return;
            };
            if (((!(Global.isVisiting())) && (hasValidId()))){
                this.createCoinPickEffect();
            };
        }
        protected function updateEffects():void{
            if (((!(isNeedingRoad)) && (!(isUxLockedByQuests)))){
                this.createCoinPickEffect();
                return;
            };
            if (this.m_coinPickEffect){
                this.m_coinPickEffect.cleanUp();
                this.m_coinPickEffect = null;
            };
        }
        override public function replaceContent(_arg1:DisplayObject):void{
            super.replaceContent(_arg1);
            this.updateEffects();
        }
        protected function unloadTourists():void{
            Global.world.citySim.npcManager.createBusinessPeepsWaiting(this, 3);
        }
        public function performVisit(_arg1:Peep):void{
        }
        public function performVisitAnimation(_arg1:Peep):void{
            if (!isNeedingRoad){
                this.createCoinPickEffect();
                this.m_coinPickEffect.setBounceType(_arg1.getBusinessEntranceEffects(this));
                this.m_coinPickEffect.bounceToType(CoinPickEffect.PICK_1);
                createStagePickEffect();
            };
        }

    }
}//package Classes 
